package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.BrowseFootAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.GoodsHistoryBean;
import com.yiwei.zxp.api.bean.GoodsHistoryDeleteBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.event.TimeSelectEvent;
import com.yiwei.zxp.util.DateTime;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.OnLoadeMoreListener;
import com.yiwei.zxp.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class BrowseFootActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_time_select)
    TextView txtTimeSelect;
    @BindView(R.id.txt_time_now)
    TextView txtTimeNow;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.img_check)
    ImageView imgCheck;
    @BindView(R.id.btn_delete)
    TextView btnDelete;
    @BindView(R.id.layout_select)
    LinearLayout layoutSelect;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private int selectDay = 0;
    private Unbinder unbiner;
    private BrowseFootAdapter adapter;
    private OnLoadeMoreListener listener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getGoodsHistory(page, selectDay);
        }
    };
    private boolean isCheckAll = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_foot);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
        getGoodsHistory(1, 0);
        EventBus.getDefault().register(this);
    }

    private void init() {
        txtTitle.setText("浏览足迹");
        txtRight.setText("编辑");
        txtRight.setVisibility(View.VISIBLE);
        list.addOnScrollListener(listener);
        adapter = new BrowseFootAdapter(this);
        adapter.setOnChooseListener(new BrowseFootAdapter.OnChooseListener() {
            @Override
            public void onDelete(int poi, String goods_id) {
                getDeleteHistory(poi, goods_id);

            }
        });
        list.setAdapter(adapter);
        txtTimeNow.setText(DateTime.toMonthDay(new Date()));
    }

    public void getGoodsHistory(int page, int day) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        GoodsHistoryBean bean = new GoodsHistoryBean();
        bean.day = day;
        bean.page = page;
        Api.api_service.getGoodsHistory(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    public void getDeleteHistory(int poi, String id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        GoodsHistoryDeleteBean bean = new GoodsHistoryDeleteBean();
        bean.id = id;
        Api.api_service.getGoodsHistoryDelete(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "删除成功");
                    listener.setPage(1);
                    getGoodsHistory(1, selectDay);
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(TimeSelectEvent event) {
        selectDay = event.day;
        txtTimeSelect.setText(event.name);
        getGoodsHistory(1, selectDay);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.layout_time_select, R.id.btn_back, R.id.txt_right, R.id.img_check, R.id.btn_delete})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.txt_right:
                if (UI.toString(txtRight).equals("编辑")) {
                    layoutSelect.setVisibility(View.VISIBLE);
                    txtRight.setText("完成");
                    adapter.setShowLeft(true);
                } else {
                    layoutSelect.setVisibility(View.GONE);
                    txtRight.setText("编辑");
                    adapter.setShowLeft(false);
                }
                break;
            case R.id.layout_time_select:
                Util.startActivity(this, TimeSelectActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.img_check:
                isCheckAll = !isCheckAll;
                if (isCheckAll) {
                    imgCheck.setImageResource(R.mipmap.img_check_box_check);
                    adapter.setAllCheck(true);
                } else {
                    imgCheck.setImageResource(R.mipmap.img_check_box_uncheck);
                    adapter.setAllCheck(false);
                }
                break;
            case R.id.btn_delete:
                if (TextUtils.isEmpty(adapter.getGoodsId())) {
                    ToastUtil.makeText(this, "请选择要删除的浏览足迹");
                    return;
                }
                getDeleteHistory(-1, adapter.getGoodsId());
                break;
        }
    }
}
